﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tedds_Tool.RulesBase;
using System.Windows.Automation;
using Tedds_Tool;
using System.IO;
using Tedds_Tool.Automatables;
using CommonInterfaces.Models;
using LEET.CommonInterfaces.Models;

namespace RulesBase.Model
{
    class Precondition : IPrecondition
    {
        public Precondition(String name, IList<IInstruction> instructions)
        {
            Name = name;
            Instructions = instructions;
        }

        public void Trigger(AutomationElement ae)
        {
            throw new NotImplementedException();
        }

        public string Name
        {
            get;
            private set;
        }

        public IList<IInstruction> Instructions
        {
            get;
            private set;
        }

        internal static IPrecondition Load(string name, IInstructionFactory fac, IScriptFinder finder)
        {
            IPrecondition condition = null;
            if (!string.IsNullOrEmpty(name))
            {
                using (TextReader reader = finder.FindPrecondition(name))
                {
                    IList<IInstruction> instructions = new List<IInstruction>();
                    String ins;
                    while ((ins = reader.ReadLine()) != null)
                    {
                        instructions.Add(fac.Interpret(ins));
                    }
                    condition = new Precondition(name, instructions);
                }
            }
            return condition;

        }
    }
}
